// pages/myClass/myClass.js
var util = require('../../utils/util.js');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    weeks: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
    days: [],
    year: 0,
    mouth: 0,
    nowDate: '',
    classData:[
      { id: 1, date: '2021-2-19', detail:'明天有雨'},
      { id: 2, date: '2021-3-20', detail: '明天有雨' },
      { id: 3, date: '2021-1-19', detail: '今天是周三' },
      { id: 4, date: '2020-8-2', detail: '明天有雨' },
      { id: 5, date: '2021-9-19', detail: '今天是周三' },
      { id: 6, date: '2021-3-7', detail: '明天有雨' }
    ],
    isClassDataDateDay:false,
    markDetailArr: [],
    dayDetail:[],
    classesD:{
      id: 0, date: '2021.3.6-8', name: '暑假班3日小程序课堂', address: 'xx区 xx路xx号', clickShowClassCode: false
    },
    keyColor:null
  },
  // 获取数据库的时间和内容
  getMarkTime:function(){
    var day = [];
    for (var i = 0; i < this.data.classData.length; i++) {
      var day1 = this.data.classData[i].date.split('-');
      day.push(day1);
      var markDetail1 = this.data.classData[i].detail;
      var obj = {};
      obj.year = day[i][0];
      obj.mouth = day[i][1];
      obj.day = day[i][2];
      obj.markDetail = markDetail1;
      this.data.markDetailArr.push(obj)
    }
    // console.log(this.data.markDetailArr)
  },
  // 提示弹框
  showModal1(){
    wx.showModal({
      title: '提醒',
      content: '该课程上课时间已过，或您当天没课。',
      showCancel: false,
      confirmText: '了解',
      success: function (res) {
        if (res.confirm) {
          // console.log('用户点击了“了解”')
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMarkTime();
    let that = this
    that.dateData();
    var myDate = new Date();//获取系统当前时间
    var nowDate = myDate.getDate();
    that.setData({
      nowDate: nowDate,
    })

  },
  // for循环  有课称的日期进行操作
  forDate:function(daysArr, dayS){
    for(var i = 0; i<daysArr.length; i++) {
      if (daysArr[i].clickShowClassCode){
        daysArr[i].clickShowClassCode = false;
      }else {
        if (daysArr[i].day == dayS[2] && daysArr[i].color) {
          // wx.navigateTo({
          //   url: daysArr[i].urlD
          // })
          daysArr[i].clickShowClassCode = true;
        }
      }
    }
    this.setData({
      days: daysArr,
    })
  },
  // 点击日期事件：能够进入到想要的课程详情页面
  selDate: function (e) {
    // 当前日期
    var timeD = util.formatDate(new Date());
    var dayN = timeD.split('/');
    // console.log(dayN);
    // 点击的日期
    var selday = e.currentTarget.dataset.day;
    var dayS = selday.split('-');
    // 页面的日期绑定的数组
    var daysArr = this.data.days
    // 循环页面的日期，将页面日期与绑定的日期进行对比，相同则跳转
      if (parseInt(dayS[0]) >= parseInt(dayN[0]) && parseInt(dayS[1]) >= parseInt(dayN[1]) ){
        // 当月日期判断天
        if (parseInt(dayS[0]) == parseInt(dayN[0]) && parseInt(dayS[1]) == parseInt(dayN[1]) && parseInt(dayS[2]) >= parseInt(dayN[2]) ){
          this.forDate(daysArr, dayS)
          // 下月日期判断月
        } else if (parseInt(dayS[0]) >= parseInt(dayN[0]) && parseInt(dayS[1]) > parseInt(dayN[1])){
          this.forDate(daysArr, dayS)
          // 下年日期判断年
        } else if (parseInt(dayS[0]) > parseInt(dayN[0]) ){
          this.forDate(daysArr, dayS);
        }else {
          this.showModal1();
        }
      } else {
        this.showModal1();
      }

      // 判断有数据且日期大于当前日期
  },

  //用户点击减少月份
  minusMouth: function () {
    var mouth;
    var year;
    mouth = this.data.mouth
    year = this.data.year
    mouth--
    if (mouth < 1) {
      mouth = 12
      year--
    }
    this.updateDays(year, mouth)
  },
  //用户点击增加月份
  plusMouth: function () {
    var mouth;
    var year;
    mouth = this.data.mouth
    year = this.data.year
    mouth++
    if (mouth > 12) {
      mouth = 1
      year++
    }
    this.updateDays(year, mouth)
  },
  dateData: function () {
    var date = new Date();
    var days = [];
    var year = date.getFullYear();
    var mouth = date.getMonth() + 1;
    this.updateDays(year, mouth)

  },
  updateDays: function (year, mouth) {
    var days = [];
    var dateDay, dateWeek;
    // 根据日期获取每个月有多少天
    var getDateDay = function (year, mouth) {
      return new Date(year, mouth, 0).getDate();
    }
    //根据日期获取这天是周几
    var getDateWeek = function (year, mouth) {
      return new Date(year, mouth - 1, 1).getDay();
    }

    dateDay = getDateDay(year, mouth)
    dateWeek = getDateWeek(year, mouth)
    //向数组中添加天
    for (let index = 1; index <= dateDay; index++) {
      var index1 = {};
      index1.day = index;
      for (var i = 0; i < this.data.markDetailArr.length;i++){
        if (this.data.markDetailArr[i].year == year && this.data.markDetailArr[i].mouth == mouth && this.data.markDetailArr[i].day == index) {
          index1.detail=this.data.markDetailArr[i].markDetail;
          // if (days[index].day == this.data.markDetailArr[i].day){
          //   index1.detail1 = this.data.markDetailArr[i].markDetail;
          // }
            // index1.urlD = "../courseDetail/courseDetail";
            index1.color = true;
            index1.clickShowClassCode = false;
            break
        } else {

        }
      
      }
      days.push(index1);
    }
    // console.log(days)
    //向数组中添加，一号之前应该空出的空格
    for (let index = 1; index <= dateWeek; index++) {
      days.unshift(0)
    }
    this.setData({
      days: days,
      year: year,
      mouth: mouth,
    })
  },
  // clickShowClass: function () {
  //   console.log('1')
  //   this.setData({
  //     clickShowClassCode:true
  //   })
  // },
  tagChoose:function(options){
    var days1 = options.currentTarget.dataset.id;
    this.setData({
      keyColor: days1
    })
  }
})